package com.yunquan.ohana.ui.calendar.sync.syncNew.inputURL

import android.content.Intent
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseActivity
import com.yunquan.ohana.databinding.ActivitySyncInputUrlBinding
import com.yunquan.ohana.utils.StringUtils
import com.yunquan.ohana.utils.ToastUtils
import com.yunquan.ohana.utils.Utils
import java.util.Locale

class InputURLActivity : BaseActivity<ActivitySyncInputUrlBinding, InputURLViewModel>() {
    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_sync_input_url
    }

    override fun initVariableId(): Int {
        return BR.inputURLViewModel
    }

    override fun initParam() {
        super.initParam()
        Locale.setDefault(Locale("en"))
        // 设置背景颜色
        window.statusBarColor = ContextCompat.getColor(this, R.color.white)
        // 设置文字颜色（true = 黑色文字，false = 白色文字）
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.isAppearanceLightStatusBars = true
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        binding!!.etUrlIcs.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                Utils.instance.hideKeyboard(v)
                onBack()
                return@setOnEditorActionListener true
            }
            return@setOnEditorActionListener false
        }
        binding!!.btnDone.setOnClickListener { onBack() }
    }

    private fun onBack() {
        val url = binding!!.etUrlIcs.text.toString()
        if (StringUtils.isEmpty(url)) {
            ToastUtils.showShort(R.string.tip_url_ics_hint)
            return
        }
        val intent = Intent()
        intent.putExtra("url", url)
        setResult(1001, intent)
        finish()
    }
}